<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品分类</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <el-button
        type="primary"
        class="bottom"
        @click="addGoods"
      >添加分类</el-button>
      <tree-table
        :data="result"
        :columns="columns"
        :selection-type="false"
        border
        :expand-type="false"
        :show-row-hover="false"
        show-index
        index-text="#"
      >
        <template
          slot="isok"
          slot-scope="scope"
        >
          <i
            v-if="scope.row.cat_deleted === false"
            class="el-icon-success"
            style="color : lightgreen"
          ></i>
          <i
            v-else
            class="el-icon-error"
            style="color : red"
          ></i>
        </template>
        <template
          slot="sort"
          slot-scope="scope"
        >
          <el-tag
            v-if="scope.row.cat_level === 0"
            size="mini"
          >一级</el-tag>
          <el-tag
            v-else-if="scope.row.cat_level === 1"
            type="success"
            size="mini"
          >二级</el-tag>
          <el-tag
            v-else
            type="warning"
            size="mini"
          >三级</el-tag>
        </template>
        <template
          slot="operate"
          slot-scope="scope"
        >
          <el-button
            type="primary"
            icon="el-icon-edit"
            size="mini"
            @click="editClick(scope.row)"
          >编辑</el-button>
          <el-button
            type="danger"
            icon="el-icon-delete"
            size="mini"
            @click="deleteClick(scope.row)"
          >删除</el-button>
        </template>
      </tree-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="catList.pagenum"
        :page-sizes="[2, 5, 10, 15]"
        :page-size="catList.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <!-- 添加分类 -->
    <el-dialog
      title="添加分类"
      :visible.sync="goodsShow"
      width="50%"
      :lock-scroll="false"
      @close="addGoodsClose"
    >
      <el-form
        :model="goodsForm"
        :rules="goodsRules"
        ref="goodsRef"
        label-width="100px"
      >
        <el-form-item
          label="分类名称"
          prop="cat_name"
        >
          <el-input v-model="goodsForm.cat_name"></el-input>
        </el-form-item>
        <el-form-item label="父级名称">
          <el-cascader
            v-model="goodsKeys"
            :options="goodsList"
            :props="cascaderProps"
            @change="handleChange"
            clearable
          ></el-cascader>
        </el-form-item>
      </el-form>

      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="goodsShow = false">取 消</el-button>
        <el-button
          type="primary"
          @click="addgoodsClick"
        >确 定</el-button>
      </span>
    </el-dialog>
    <!-- 编辑对话框 -->
    <el-dialog
      title="提示"
      :visible.sync="editDialogVisible"
      width="50%"
      :lock-scroll="false"
    >
      <el-form
        ref="editFormRef"
        :model="editList"
        label-width="100px"
      >
        <el-form-item label="活动名称">
          <el-input v-model="editList.cat_name"></el-input>
        </el-form-item>
      </el-form>
      <span
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="seteditClick"
        >确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { goodsSort, addGoods, setData, deleteData} from "network/goods";
export default {
  data() {
    return {
      catList: {
        type: 3,
        pagenum: 1,
        pagesize: 5
      },
      result: [],
      total: 0,
      columns: [
        {
          label: "分类名称",
          prop: "cat_name"
        },
        {
          label: "是否有效",
          type: "template",
          template: "isok"
        },
        {
          label: "排序",
          type: "template",
          template: "sort"
        },
        {
          label: "操作",
          type: "template",
          template: "operate"
        }
      ],
      goodsShow: false,
      editDialogVisible: false,

      goodsForm: {
        cat_name: "",
        cat_pid: 0,
        cat_level: 0
      },
      cascaderProps: {
        expandTrigger: "hover",
        label: "cat_name",
        value: "cat_id",
        children: "children",
        checkStrictly: true
      },
      goodsRules: {
        cat_name: [
          { required: true, message: "请输入分类名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" }
        ]
      },

      goodsList: [],
      goodsKeys: [],

      editList: {},
      editId: null
    };
  },
  methods: {
    getData() {
      goodsSort("/categories", this.catList).then(res => {
        // console.log(res);
        if (res.data.meta.status === 200) {
          this.result = res.data.data.result;
          this.total = res.data.data.total;
        } else {
          this.$Message.error("获取失败！");
        }
      });
    },
    handleSizeChange(size) {
      this.catList.pagesize = size;
      this.getData();
    },
    handleCurrentChange(num) {
      this.catList.pagenum = num;
      this.getData();
    },
    addGoods() {
      this.goodsShow = true;
      this.getGoodsList();
    },
    getGoodsList() {
      goodsSort("categories", { type: 2 }).then(res => {
        // console.log(res)
        if (res.data.meta.status === 200) {
          this.goodsList = res.data.data;
          // console.log(this.goodsList);
        } else {
          this.$Message.error("数据获取失败！");
        }
      });
    },
    handleChange() {
      if (this.goodsKeys.length === 0) {
        this.goodsForm.cat_level = 0;
        this.goodsForm.cat_pid = 0;
      } else {
        this.goodsForm.cat_pid = this.goodsKeys[this.goodsKeys.length - 1];
        this.goodsForm.cat_level = this.goodsKeys.length;
      }
      console.log(this.goodsKeys);
    },
    addgoodsClick() {
      // console.log(this.goodsForm)
      addGoods("/categories", this.goodsForm).then(res => {
        // console.log(res)
        if (res.data.meta.status === 201) {
          this.getData();
          this.$Message.success(res.data.meta.msg);
          this.goodsShow = false;
        } else {
          this.$Message.error(res.data.meta.msg);
        }
      });
    },
    addGoodsClose() {
      this.$refs.goodsRef.resetFields();
      this.goodsKeys = [];
      this.goodsForm.cat_pid = 0;
      this.goodsForm.cat_level = 0;
    },

    editClick(data) {
      this.editDialogVisible = true;
      this.editId = data.cat_id;

      goodsSort(`categories/${data.cat_id}`).then(res => {
        // console.log(res)
        if (res.data.meta.status === 200) {
          this.editList = res.data.data;
        }
      });
    },
    seteditClick() {
      setData(`categories/${this.editId}`, {
        cat_name: this.editList.cat_name
      }).then(res => {
        if (res.data.meta.status === 200) {
          this.getData();
          this.editDialogVisible = false;
          this.$Message.success(res.data.meta.msg);
        } else {
          this.$Message.error(res.data.meta.msg);
        }
      });
    },

    async deleteClick(data) {
      const isTrue = await this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).catch(err => err)
      console.log(isTrue)
      if(isTrue === 'cancel'){
        this.$Message.info('取消删除！')
      }else{
        deleteData(`categories/${data.cat_id}`).then(res => {
          if(res.data.meta.status === 200){
            this.getData()
            this.$Message.success(res.data.meta.msg)
          }else{
            this.$Message.error(res.data.meta.msg)
          }
        })
      }
      
    }
  },
  created() {
    this.getData();
  }
};
</script>
<style lang="less" scoped>
.el-card {
  margin: 20px 0;
  .bottom {
    margin-bottom: 15px;
  }
}
.el-cascader {
  width: 100%;
}
</style>